-- 文件
local gg         = require("modules.business_welfare.welfare_gg")
local config     = require("modules.business_welfare.welfare_lua_config")
local uu         = require("modules.commhm.utils")
local ns_account = require("modules.business_welfare.welfare_bridge")
local log        = require("modules.commhm.log")
local code_conf  = require("modules.business_welfare.welfare_code")
local op_log     = require("modules.business_welfare.welfare_op_log")
local g_enum     = require("modules.business_welfare.welfare_enum")
local bridge     = require("modules.business_common.business_account")
local misc       = require("modules.business_common.business_utils")
local account    = require("modules.business_common.business_account")

-- 变量 开头定义优先级 系统库 > ngx > 自定义
local pairs      = pairs
local ipairs     = ipairs
local math       = math
local next       = next
local table      = table
local tonumber   = tonumber

local ngx = ngx

local error_code = code_conf.error_code
local account2 = ns_account.private_account

local FOO = {
    s_name = string.match(debug.getinfo(1).short_src, '(%w+).lua')
}

--接口有效性校验
FOO.pre_skin_sale = function(t_cfg)
    local t_params        = ngx.ctx.m_params
    local s_activityId    = t_params.activity_id
    local n_code          = FOO.check_conf(t_cfg, s_activityId)

    if n_code ~= error_code.COMMON_SUCCESS then
        return n_code
    end

    return error_code.COMMON_SUCCESS
end

FOO.get_skin_cfg = function()
    local t_cfg = config.get_cfg('welfare_skin_sale')
    return t_cfg
end

-- 配置检查
FOO.check_conf = function(t_cfg, s_activityId)
    if not (t_cfg and t_cfg.t_skinLabels and t_cfg.n_startTime and t_cfg.n_endTime ) then
        return error_code.ERROR_CONFIG
    end

    if not t_cfg.b_switch then
        return error_code.SKIN_ACTIVITY_END
    end

    for _, condition_check in pairs(FOO.condition_list or {}) do
        local n_code = condition_check(t_cfg)
        if n_code ~= error_code.COMMON_SUCCESS then
            return n_code
        end
    end

    if s_activityId then
        local t_activity = t_cfg.t_skinLabels[s_activityId]
        if not (t_activity.s_activityId and t_activity.n_skinId and t_activity.t_bonus) then
            return error_code.ERROR_CONFIG
        end
        for _, condition_check in pairs(FOO.condition_list or {}) do
            local n_code = condition_check(t_activity)
            if n_code ~= error_code.COMMON_SUCCESS then
                return n_code
            end
        end
    end

    return error_code.COMMON_SUCCESS
end

-- 进入界面拉取的数据
FOO.get_skin_sale_data = function()
    local t_cfg      = FOO.get_skin_cfg()
    local n_code     = FOO.pre_skin_sale(t_cfg)
    local t_userData = nil
    local t_ret      = {}

    if n_code ~= error_code.COMMON_SUCCESS then
        return n_code
    end

    t_userData = gg.get_ssdb_cache_info(gg.KEYS.SKIN_SALE, 1) or {}

    if not t_userData then
        return error_code.ERROR_NO_DATA
    end

    FOO.clean_expired_data(t_cfg, t_userData)

    gg.set_ssdb_cache_info(gg.KEYS.SKIN_SALE, t_userData)

    for k, v in pairs(t_userData) do
        if type(v) == 'table' and v.stat then
            local n_len = #t_ret + 1
            t_ret[n_len] = k
        end
    end

    log.loggerdebug("skin sale all data : data = %%", t_userData)

    return error_code.COMMON_SUCCESS, t_ret
end

-- 领取奖励
FOO.get_skin_sale_award = function()
    local t_params        = ngx.ctx.m_params
    local s_activityId    = t_params.activity_id
    local n_skinId        = 0
    local t_cfg           = FOO.get_skin_cfg()
    local n_code          = FOO.pre_skin_sale(t_cfg)
    local t_ret           = {}
    local t_userData, t_activity, t_bonus

    if not s_activityId then
        return error_code.ERROR_PARAM
    end

    if n_code ~= error_code.COMMON_SUCCESS then
        return n_code
    end

    if not (t_cfg.t_skinLabels and t_cfg.t_skinLabels[s_activityId]) then
        return error_code.ERROR_PARAM
    end

    t_userData = gg.get_ssdb_cache_info(gg.KEYS.SKIN_SALE)
    if not t_userData then
        return error_code.ERROR_NO_DATA
    end

    n_skinId                 = t_cfg.t_skinLabels[s_activityId].n_skinId
    t_userData[s_activityId] = t_userData[s_activityId] or {}
    t_activity               = t_userData[s_activityId]
    if t_activity.stat and t_activity.stat == 1 then
        return error_code.ALREADY_GET_REWARDS
    end

    if not FOO.check_owned_skin(n_skinId) then
        return error_code.NOT_OWNED_SKIN
    end

    t_bonus = t_cfg.t_skinLabels[s_activityId].t_bonus
    if FOO.do_rewards(t_bonus) ~= error_code.COMMON_SUCCESS then
        return error_code.ERROR_ADD_ITEM
    end

    t_activity.stat     = 1
    t_activity.ctime    = ngx.time()
    t_activity.bonus    = t_bonus

    gg.set_ssdb_cache_info(gg.KEYS.SKIN_SALE, t_userData)

    for k, v in pairs(t_userData) do
        if type(v) == 'table' and v.stat then
            t_ret[k] = v
        end
    end

    log.loggerdebug("skin sale all data : data = %%", t_userData)

    return error_code.COMMON_SUCCESS, t_ret
end

-- 领取奖励逻辑
FOO.do_rewards = function(t_bonus)
    if not next(t_bonus or {}) then
        return error_code.ERROR_ADD_ITEM
    end

    local ret = account2.items_add(t_bonus, "skin_sale_rewards")
    if not ret then
        log.loggerdebug("skin sale rewards error, ret = %%", ret)
        return error_code.ERROR_ADD_ITEM
    end

    op_log.record("skin_sale", 'get_skin_sale_award', t_bonus)

    return error_code.COMMON_SUCCESS
end

-- 条件判定
FOO.condition_list = {
    [1] = function(t_cfg)       --活动有效期间
        local t_params  = ngx.ctx.m_params
        local n_now     = ngx.time()
        if n_now < t_cfg.n_startTime or n_now > t_cfg.n_endTime then
            log.day_list("skin_sale_error", "skin sale condition_list out of time|" .. t_params.uin)
            return error_code.SKIN_ACTIVITY_END
        end
        return error_code.COMMON_SUCCESS
    end,

    [2] = function(t_cfg)       -- 渠道控制
        if uu.tableNotEmpty(t_cfg.t_apiids) then
            local t_params      = ngx.ctx.m_params
            local n_apiid       =  tonumber(t_params.apiid or 0)
            local b_valid_apiid = false

            for _, v in pairs(t_cfg.t_apiids) do
                if v and tonumber(v) == n_apiid then
                    b_valid_apiid = true
                    break
                end
            end
            if not b_valid_apiid then
                log.day_list("skin_sale_error","skin sale apiid is invalid|" .. t_params.uin .. "|" .. n_apiid)
                return error_code.INVALID_APIID
            end
        end
        return error_code.COMMON_SUCCESS
    end,

    [3] = function(t_cfg)
        local t_params  = ngx.ctx.m_params
        local n_l       = uu.ver2i(t_params.ver or 0)
        local n_r       = t_cfg.n_versionMin
        if n_l < n_r then
            log.day_list("skin_sale_error","skin sale version too low|" .. t_params.uin .. "|" .. n_l)
            return error_code.ERROR_CLIENT_VERSION
        end
        return error_code.COMMON_SUCCESS
    end
}

FOO.clean_expired_data = function(t_cfg, t_data)
    for k, v in pairs(t_data or {}) do
        local t_activity = t_cfg.t_skinLabels[k]
        if t_activity and t_activity.s_activityId and k == t_activity.s_activityId then
            if  v.ctime < t_activity.n_startTime or v.ctime > t_activity.n_endTime then
                t_data[k] = nil
            end
        else
            t_data[k] = nil
        end
    end
end

--! @ 查询已拥有的永久皮肤
FOO.check_owned_skin = function(n_skinId)
    local ret = false
    local rst, list = account.query_owned_skin_account()
    if not rst then
        log.error('gift query skin error')
        return ret
    end
    for _, v in pairs(list or {}) do
        log.day_list("skin_sale", v, n_skinId)
        if v == n_skinId then
            ret = true
            break
        end
    end
    return ret
end

return FOO

